package cool.tenyears.admin.service;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.lang.Assert;
import cool.tenyears.admin.domain.LoginUserDto;
import cool.tenyears.common.constant.UserConstant;
import cool.tenyears.common.method.UserMethod;
import cool.tenyears.domain.User;
import cool.tenyears.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * @author LiChuan
 * @date 2021/7/12 12:07
 */
@Service
@RequiredArgsConstructor
public class AdminUserService {
    private final UserService userService;

    public void login(LoginUserDto loginUser) {
        User user = userService.getByLoginName(loginUser.getSign());
        Assert.notNull(user, "用户名/手机号/邮箱或密码错误！");
        Assert.isTrue(UserMethod.checkPassword(user, loginUser.getPassword()), "用户名/手机号/邮箱或密码错误！");
        Assert.isTrue(UserMethod.userIsOk(user), "该用户已被禁用，请联系管理员！");
        StpUtil.login(user.getId() + UserConstant.LOGIN_ID_SEPARATOR + user.getLoginUser());
        StpUtil.getSession().set(user.getId(), user);
    }
}
